<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="订单号" prop="payOrdernumber">
        <el-input
          v-model="queryParams.payOrdernumber"
          placeholder="请输入支付订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入维保企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus">
          <el-option
            v-for="(item, index) in Dictionary.classifyPlanStatus"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="维保地址" prop="businessAddress">
        <el-input
          v-model="queryParams.businessAddress"
          placeholder="请输入维保企业地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="订单开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="订单结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wb:wbRepairOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wb:wbRepairOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wb:wbRepairOrder:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <!-- <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wb:wbRepairOrder:export']">导出</el-button> -->
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wbRepairOrderList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="区域id" align="center" prop="regionId" /> -->
      <!-- <el-table-column label="区域名称" align="center" prop="regionName" /> -->
      <!-- <el-table-column label="维保企业id" align="center" prop="businessId" /> -->
      <el-table-column
        label="订单号"
        align="center"
        prop="payOrdernumber"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="业主公司"
        align="center"
        prop="ownerCompanyName"
      />
      <!-- <el-table-column label="业主企业地址" align="center" prop="businessAddress" /> -->
      <el-table-column label="维修师傅" align="center" prop="repairerId">
        <template slot-scope="{ row }">
          <span>{{ row.repairerId | filterMaster }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{
            fomatterLabelName("classifyPlanStatus", scope.row.status)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上门时间"
        align="center"
        prop="arriveTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(
              scope.row.newArriveTime || scope.row.subscribeDate,
              "{y}-{m}-{d}"
            )
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="维修工时(h)" align="center" prop="manHour" />
      <el-table-column label="工时费用(元)" align="center" prop="manHourCost" />
      <!-- <el-table-column label="设备更换信息" align="center" prop="deviceRenewInfo" /> -->
      <el-table-column label="实付金额(元)" align="center" prop="payMoney" />
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applyerName" />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wb:wbRepairOrder:edit']"
            >查看</el-button
          >
          <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                :disabled="scope.row.status != 6"
                @click="allocationMaster(scope.row, 1)"
                v-hasPermi="['wb:wbRepairOrder:edit']"
            >分配师傅</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wb:wbRepairOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wb:wbRepairOrder:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="710px" append-to-body>
      <!-- <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区域id" prop="regionId">
          <el-input v-model="form.regionId" placeholder="请输入区域id" />
        </el-form-item>
        <el-form-item label="区域名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="维保企业id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入维保企业id" />
        </el-form-item>
        <el-form-item label="维保企业名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入维保企业名称" />
        </el-form-item>
        <el-form-item label="维保企业地址" prop="businessAddress">
          <el-input v-model="form.businessAddress" placeholder="请输入维保企业地址" />
        </el-form-item>
        <el-form-item label="维保/维修申请id" prop="applyId">
          <el-input v-model="form.applyId" placeholder="请输入维保/维修申请id" />
        </el-form-item>
        <el-form-item label="维修者id" prop="repairerId">
          <el-input v-model="form.repairerId" placeholder="请输入维修者id" />
        </el-form-item>
        <el-form-item label="上门时间" prop="arriveTime">
          <el-date-picker clearable
            v-model="form.arriveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上门时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="维修工时" prop="manHour">
          <el-input v-model="form.manHour" placeholder="请输入维修工时" />
        </el-form-item>
        <el-form-item label="工时费用" prop="manHourCost">
          <el-input v-model="form.manHourCost" placeholder="请输入工时费用" />
        </el-form-item>
        <el-form-item label="设备更换信息" prop="deviceRenewInfo">
          <el-input v-model="form.deviceRenewInfo" placeholder="请输入设备更换信息" />
        </el-form-item>
        <el-form-item label="实付金额" prop="payMoney">
          <el-input v-model="form.payMoney" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付订单号" prop="payOrdernumber">
          <el-input v-model="form.payOrdernumber" placeholder="请输入支付订单号" />
        </el-form-item>
      </el-form> -->
      <div class="info-content">
        <div class="info-title">基本信息</div>
        <!-- 基本信息 -->
        <div class="base-info">
          <div class="info-item">
            <span>业主公司：</span> {{ form.ownerCompanyName }}
          </div>
          <div class="info-item">
            <span>申请人：</span> {{ form.applyerName }}
          </div>
          <div class="info-item"><span>电话：</span>{{ form.phoneNumber }}</div>
          <div class="info-item-row">
            <span>地址：</span> {{ form.address }}
          </div>
          <div class="info-item-row">
            <span>故障描述：</span> {{ form.remark }}
          </div>
          <div class="info-item-row">
            <span>故障图片：</span>
            <div></div>
          </div>
        </div>
        <div class="info-title">维修信息</div>
        <div class="base-info">
          <!-- 报价信息 -->
          <div class="info-item">
            <span>维修状态：</span>
            {{ fomatterLabelName("classifyPlanStatus", form.status) }}
          </div>
          <div class="info-item">
            <span>维修师傅：</span> {{ form.repairerId | filterMaster }}
          </div>
          <div class="info-item">
            <span>维修工时：</span> {{ form.manHour }}(h)
          </div>
          <div class="info-item">
            <span>维修工时费用：</span> {{ form.manHourCost }}(元)
          </div>
          <div class="info-item">
            <span>设备更换信息：</span>
            {{
              form.deviceRenewInfoDtos && form.deviceRenewInfoDtos.length > 0
                ? form.deviceRenewInfoDtos
                    .map((ele) => ele.deviceRenewInfo)
                    .join(",")
                : ""
            }}
          </div>
        </div>
        <div class="info-title">支付信息</div>
        <div class="base-info">
          <!-- 联系信息 -->
          <div class="info-item">
            <span>支付金额：</span> {{ form.payMoney }}(元)
          </div>
          <div class="info-item">
            <span>支付时间：</span> {{ parseTime(form.payTime, "{y}-{m}-{d}") }}
          </div>
          <div class="info-item">
            <span>订单号：</span> {{ form.payOrdernumber }}
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="isMasterOpen"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :rules="rules" label-width="80px" :inline="true">
        <el-form-item label="师傅" prop="device">
          <el-select v-model="masterId" filterable placeholder="请选择师傅">
            <el-option
              v-for="(item, index) in masterList"
              :key="index"
              :label="item.name"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAllocationMaster"
          >确 定</el-button
        >
        <el-button @click="isMasterOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepairOrder,
  getWbRepairOrder,
  delWbRepairOrder,
  addWbRepairOrder,
  updateWbRepairOrder,
} from "@/api/wb/wbRepairOrder";
import { updateWbRepairApply } from "@/api/wb/wbRepairApply";
import { fomatterLabelName } from "@/utils/index.js";
import Dictionary from "@/utils/dictionary";
import { mapGetters, mapActions } from "vuex";
export default {
  name: "WbRepairOrder",
  data() {
    return {
      Dictionary,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修订单表格数据
      wbRepairOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regionId: null,
        regionName: null,
        businessId: null,
        businessName: null,
        businessAddress: null,
        applyId: null,
        repairerId: null,
        orderStatus: null,
        arriveTime: null,
        manHour: null,
        manHourCost: null,
        deviceRenewInfo: null,
        payMoney: null,
        payTime: null,
        payOrdernumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      isMasterOpen: false,
      masterId: "",
    };
  },
  created() {
    this.getMasterList();
    this.getList();
  },
  computed: {
    ...mapGetters(["masterList"]),
  },
  methods: {
    fomatterLabelName,
    ...mapActions("reqSelect", ["getMasterList"]),
    /** 查询维修订单列表 */
    getList() {
      this.loading = true;
      listRepairOrder(this.queryParams).then((response) => {
        let list = response.rows;
        this.wbRepairOrderList = list.map((ele) => {
          return {
            ...ele.wbRepairApply,
            id: ele.id,
            repairId: (ele.wbRepairApply && ele.wbRepairApply.id) || null,
            manHour: ele.manHour,
            manHourCost: ele.manHourCost,
            deviceRenewInfoDtos: ele.deviceRenewInfoDtos,
            payMoney: ele.payMoney,
            payTime: ele.payTime,
            payOrdernumber: ele.payOrdernumber,
            repairerId: ele.repairerId,
          };
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    allocationMaster(row) {
      this.reset();
      this.masterId = "";
      const id = row.id || this.ids;
      getWbRepairOrder(id).then((response) => {
        // this.form = response.data;
        let ele = response.data;
        this.form = {
          ...ele.wbRepairApply,
          id: ele.id,
          repairId: (ele.wbRepairApply && ele.wbRepairApply.id) || null,
          manHour: ele.manHour,
          manHourCost: ele.manHourCost,
          deviceRenewInfoDtos: ele.deviceRenewInfoDtos,
          payMoney: ele.payMoney,
          payTime: ele.payTime,
          payOrdernumber: ele.payOrdernumber,
          repairerId: ele.repairerId,
        };
        this.isMasterOpen = true;
        this.title = "分配师傅";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        regionId: null,
        regionName: null,
        businessId: null,
        businessName: null,
        businessAddress: null,
        applyId: null,
        repairerId: null,
        orderStatus: null,
        arriveTime: null,
        createTime: null,
        updateTime: null,
        remark: null,
        manHour: null,
        manHourCost: null,
        deviceRenewInfo: null,
        payMoney: null,
        payTime: null,
        payOrdernumber: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWbRepairOrder(id).then((response) => {
        // this.form = response.data;
        let ele = response.data;
        this.form = {
          ...ele.wbRepairApply,
          id: ele.id,
          repairId: (ele.wbRepairApply && ele.wbRepairApply.id) || null,
          manHour: ele.manHour,
          manHourCost: ele.manHourCost,
          deviceRenewInfoDtos: ele.deviceRenewInfoDtos,
          payMoney: ele.payMoney,
          payTime: ele.payTime,
          payOrdernumber: ele.payOrdernumber,
          repairerId: ele.repairerId,
        };
        this.open = true;
        this.title = "查看维修订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWbRepairOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWbRepairOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitAllocationMaster() {
      if (!this.masterId || this.masterId == "") {
        this.$modal.msgWarning("请选择师傅");
        return;
      }
      updateWbRepairApply({
        id: this.form.repairId,
        status: 1,
        repairerId: this.masterId,
      }).then((res) => {
        if (res.code == 200) {
          this.$modal.msgSuccess("分配成功");
          this.isMasterOpen = false;
          this.getList();
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除维修订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWbRepairOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbRepairOrder/export",
        {
          ...this.queryParams,
        },
        `wbRepairOrder_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.info-content {
  font-size: 14px;

  .base-info {
    display: flex;
    flex-wrap: wrap;
    width: 800px;
  }

  .info-item {
    width: 30%;
    flex: 0 0 30%;
    margin-bottom: 15px;

    span {
      font-weight: 600;
    }
  }

  .info-item-row {
    width: 50%;
    flex: 0 0 50%;
    margin-bottom: 15px;

    span {
      font-weight: 600;
    }
  }
}

.other-info {
  display: flex;
  margin-top: 30px;

  .info-table {
    margin-left: 30px;

    &:first-of-type {
      margin-left: 0;
    }
  }
}

.info-title {
  font-size: 18px;
  margin-bottom: 10px;

  &::before {
    content: "";
    display: inline-block;
    width: 3px;
    height: 15px;
    background: #1777ff;
    border-radius: 3px;
    margin-right: 8px;
  }
}
</style>
